﻿using System;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

using QLNHK.BLL;

namespace QLNHK.BLL {
	public class Cls_NHOM_NGUOIDUNG_CHITIET
	{
		private const string ST_NHOM_NGUOIDUNG_CHITIET_INSERT = "st_NHOM_NGUOIDUNG_CHITIETInsert";
		private const string ST_NHOM_NGUOIDUNG_CHITIET_UPDATE = "st_NHOM_NGUOIDUNG_CHITIETUpdate";
		private const string ST_NHOM_NGUOIDUNG_CHITIET_DELETE = "st_NHOM_NGUOIDUNG_CHITIETDelete";
		private const string ST_NHOM_NGUOIDUNG_CHITIET_SELECTALL = "st_NHOM_NGUOIDUNG_CHITIETSELECTALL";
		private const string ST_NHOM_NGUOIDUNG_CHITIET_DELETE_BY_ID_NHOM = "st_NHOM_NGUOIDUNG_CHITIETDeleteByIDNhomNguoiDung";
		private const string ST_NHOM_NGUOIDUNG_CHITIET_SELECT_BY_ID_NGUOIDUNG = "st_NHOM_NGUOIDUNG_CHITIETSELECTByIDNguoiDung";
		private const string TABLE_NAME = "tb_NHOM_NGUOIDUNG_CHITIET";
		private Database db;
		private int _iD_Nhom_NguoiDung;
		private int _iD_NguoiDung;
		public int ID_Nhom_NguoiDung
		{
			get{return this._iD_Nhom_NguoiDung;}
			set{this._iD_Nhom_NguoiDung = value;}
		}
		public int ID_NguoiDung
		{
			get{return this._iD_NguoiDung;}
			set{this._iD_NguoiDung = value;}
		}
		public Cls_NHOM_NGUOIDUNG_CHITIET()
		{
			try
			{
				db = DatabaseFactory.CreateDatabase();
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}

		public Cls_NHOM_NGUOIDUNG_CHITIET(int id_nhom_nguoidung, int id_nguoidung)
		{
			this._iD_Nhom_NguoiDung = id_nhom_nguoidung;
			this._iD_NguoiDung = id_nguoidung;
		}
		private DbCommand InsertWrapper()
		{
			DbCommand dbCommand = db.GetStoredProcCommand(Cls_NHOM_NGUOIDUNG_CHITIET.ST_NHOM_NGUOIDUNG_CHITIET_INSERT);
			db.AddInParameter(dbCommand,"@ID_Nhom_NguoiDung", DbType.Int32,"ID_Nhom_NguoiDung", DataRowVersion.Current);
			db.AddInParameter(dbCommand,"@ID_NguoiDung", DbType.Int32,"ID_NguoiDung", DataRowVersion.Current);
			return dbCommand;
		}

		private DbCommand UpdateWrapper()
		{
			DbCommand dbCommand = db.GetStoredProcCommand(Cls_NHOM_NGUOIDUNG_CHITIET.ST_NHOM_NGUOIDUNG_CHITIET_UPDATE);
			db.AddInParameter(dbCommand,"@ID_Nhom_NguoiDung", DbType.Int32, "ID_Nhom_NguoiDung", DataRowVersion.Current);
			db.AddInParameter(dbCommand,"@ID_NguoiDung", DbType.Int32, "ID_NguoiDung", DataRowVersion.Current);
			return dbCommand;
		}

		private DbCommand DeleteWrapper()
		{
			DbCommand dbCommand = db.GetStoredProcCommand(Cls_NHOM_NGUOIDUNG_CHITIET.ST_NHOM_NGUOIDUNG_CHITIET_DELETE);
			return dbCommand;
		}

		public int DoUpdate(DataSet ds)
		{
			try
			{
				int result = db.UpdateDataSet(ds,Cls_NHOM_NGUOIDUNG_CHITIET.TABLE_NAME,this.InsertWrapper(),this.UpdateWrapper(),this.DeleteWrapper(),UpdateBehavior.Standard);
				return result;
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
		/// Author: khanhlq . Date: 2/8/2006.
		/// Modifier: khanhlq. Date:  2/8/2006.
		/// Liệt kê những gì đã chỉnh sửa:
		/// <summary>
		/// Trả về danh sách tất các các người dùng trong hệ thống.
		/// </summary>
		/// <returns>DataSet</returns>
		public DataSet ListByAll()
		{
			try
			{
				DataSet dsNhomNguoiDungChiTiet = new DataSet();
				DbCommand dbCommand = db.GetStoredProcCommand(Cls_NHOM_NGUOIDUNG_CHITIET.ST_NHOM_NGUOIDUNG_CHITIET_SELECTALL);
				db.LoadDataSet(dbCommand,dsNhomNguoiDungChiTiet,Cls_NHOM_NGUOIDUNG_CHITIET.TABLE_NAME);
				return dsNhomNguoiDungChiTiet;
			}
			catch
			{
				return null;
			}
		
		}
		public DataSet ListByIDNguoiDung(int id_nguoidung)
		{
			try
			{
				DataSet dsNhomNguoiDungChiTiet = new DataSet();
				DbCommand dbCommand = db.GetStoredProcCommand(Cls_NHOM_NGUOIDUNG_CHITIET.ST_NHOM_NGUOIDUNG_CHITIET_SELECT_BY_ID_NGUOIDUNG);
				db.AddInParameter(dbCommand,"@ID_NguoiDung", DbType.Int32, id_nguoidung);
				db.LoadDataSet(dbCommand,dsNhomNguoiDungChiTiet,Cls_NHOM_NGUOIDUNG_CHITIET.TABLE_NAME);
				return dsNhomNguoiDungChiTiet;
			}
			catch
			{
				return null;
			}
		
		}
		public Cls_NHOM_NGUOIDUNG_CHITIET[] GetGroupByUserID(int id_nguoidung)
		{
			try
			{
				
				DataSet dsNhomNguoiDungChiTiet = new DataSet();
				DbCommand dbCommand = db.GetStoredProcCommand(Cls_NHOM_NGUOIDUNG_CHITIET.ST_NHOM_NGUOIDUNG_CHITIET_SELECT_BY_ID_NGUOIDUNG);
				db.AddInParameter(dbCommand,"@ID_NguoiDung", DbType.Int32, id_nguoidung);
				db.LoadDataSet(dbCommand,dsNhomNguoiDungChiTiet,Cls_NHOM_NGUOIDUNG_CHITIET.TABLE_NAME);
				Cls_NHOM_NGUOIDUNG_CHITIET[] ObjArrNhomChiTiet = new Cls_NHOM_NGUOIDUNG_CHITIET[dsNhomNguoiDungChiTiet.Tables[0].Rows.Count];
				int i=0;
				foreach(DataRow dr in dsNhomNguoiDungChiTiet.Tables[0].Rows)
				{
					Cls_NHOM_NGUOIDUNG_CHITIET ObjNhomNguoiDung = new Cls_NHOM_NGUOIDUNG_CHITIET();
					ObjNhomNguoiDung.ID_Nhom_NguoiDung = Convert.ToInt32(dr["ID_Nhom_NguoiDung"]);
					ObjArrNhomChiTiet[i] = ObjNhomNguoiDung;
					i++;
				}
				return ObjArrNhomChiTiet;
			}
			catch(Exception ex)
			{
				string a = ex.Message;
				return null;
			}
		
		}
		/// Author: khanhlq . Date: 2/9/2006.
		/// Modifier: khanhlq. Date:  2/9/2006.
		/// Liệt kê những gì đã chỉnh sửa:
		/// <summary>
		/// Does the insert.
		/// </summary>
		/// <param name="id_nguoidung">The id_nguoidung.</param>
		/// <param name="id_nhom_NguoiDung">The id_nhom_ nguoi dung.</param>
		/// <returns></returns>
		public int DoInsert(int id_nguoidung, int id_nhom_NguoiDung)
		{
			try
			{
				DbCommand dbCommand = db.GetStoredProcCommand(Cls_NHOM_NGUOIDUNG_CHITIET.ST_NHOM_NGUOIDUNG_CHITIET_INSERT);
				db.AddInParameter(dbCommand,"@ID_Nhom_NguoiDung", DbType.Int32, id_nhom_NguoiDung);
				db.AddInParameter(dbCommand,"@ID_NguoiDung", DbType.Int32, id_nguoidung);
				db.ExecuteNonQuery(dbCommand);
				return 1;
			}
			catch
			{
				return -1;
			}
		}
		public int DoDelete(int intIDNguoiDung)
		{
			try
			{
				DbCommand dbCommand = db.GetStoredProcCommand(Cls_NHOM_NGUOIDUNG_CHITIET.ST_NHOM_NGUOIDUNG_CHITIET_DELETE);
				db.AddInParameter(dbCommand,"@ID_NguoiDung", DbType.Int32, intIDNguoiDung);
				return Convert.ToInt32(db.ExecuteScalar(dbCommand));
			}
			catch
			{
				return -1;
			}
		}
		/// <summary>
		/// Xóa người dùng khỏi nhóm
		/// </summary>
		/// <param name="id_nguoidung">ID người dùng</param>
		/// <param name="id_nhom_NguoiDung">ID người nhóm dùng</param>
		/// <returns>int</returns>
		public int DoDelete(int intIDNguoiDung, int intNhomIDNguoiDung)
		{
			try
			{
				 
				DbCommand dbCommand = db.GetStoredProcCommand(Cls_NHOM_NGUOIDUNG_CHITIET.ST_NHOM_NGUOIDUNG_CHITIET_DELETE_BY_ID_NHOM);
				db.AddInParameter(dbCommand,"@ID_Nhom_NguoiDung", DbType.Int32, intNhomIDNguoiDung);
				db.AddInParameter(dbCommand,"@ID_NguoiDung", DbType.String, intIDNguoiDung);
				int result = Convert.ToInt32(db.ExecuteScalar(dbCommand));
				return result;
			}
			catch(Exception ex)
			{
				throw ex;
			}

			
		}

	}

}
